package com.antrain.his.controller;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.antrain.his.annotation.UserLoginToken;
import com.antrain.his.entity.Drugstemplate;
import com.antrain.his.service.IDrugstemplateService;
import com.antrain.his.utils.InitUtil;
import com.antrain.his.utils.Result;
import com.antrain.his.utils.ResultGenerator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 *  前端控制器 成药处方模板
 * 医生把常用的成药处方存为模板，保存到此表中方便下次使用
 * </p>
 *
 * @author antrain
 * @since 2020-09-09
 */
@RestController
@RequestMapping("/drugstemplate")
public class DrugstemplateController {
  @Resource
  private IDrugstemplateService drugstemplateService;

  /**
   * 进行分页查询 
   * Name 可选 模板名称
   */
  @GetMapping
  public Result getlist(@RequestParam Map<String, Object> param) {
    InitUtil.initPage(param);
    int num = Integer.parseInt(param.get("page").toString());
    int limit = Integer.parseInt(param.get("limit").toString());
    QueryWrapper<Drugstemplate> wrapper = new QueryWrapper<>();
    InitUtil.initLike(param, wrapper, "Name");
    IPage<Drugstemplate> page = new Page<>(num, limit);
    return ResultGenerator.getSuccessResult(drugstemplateService.page(page, wrapper));
  }

  @GetMapping("/{id}")
  public Result getDrugs(@PathVariable int id) {
    Drugstemplate drugstemplate = drugstemplateService.getById(id);
    if (drugstemplate == null)
      return ResultGenerator.getFailResult("", "无该模板记录");
    return ResultGenerator.getSuccessResult(drugstemplate);
  }

  /**
   * name : 需要检查那个字段的value是否在数据库存在
   * 比如/check/DrugsName?value=阿司匹林  ：检查数据库是否存在DrugsName='阿司匹林' 
   */
  @GetMapping("/check/{name}")
  public Result checkName(@PathVariable String name,@RequestParam String value){
    QueryWrapper<Drugstemplate> wrapper = new QueryWrapper<>();
    wrapper.eq(name, value);
    if (drugstemplateService.getOne(wrapper) != null)
      return ResultGenerator.getFailResult("", "该记录已存在，请重新选择");
    return ResultGenerator.getSuccessResult();
  }

  @PostMapping()
  @UserLoginToken
  public Result save(@RequestBody Drugstemplate drugs) {
    if (drugstemplateService.save(drugs))
      return ResultGenerator.getSuccessResult("", "添加成功");
    return ResultGenerator.getFailResult("", "添加失败");
  }

  @PutMapping("/{id}")
  @UserLoginToken
  public Result update(@RequestBody Drugstemplate drugs, @PathVariable int id) {
    drugs.setId(id);
    if (drugstemplateService.updateById(drugs))
      return ResultGenerator.getSuccessResult("", "更新成功");
    return ResultGenerator.getFailResult("", "更新失败");
  }

  @DeleteMapping("/{id}")
  @UserLoginToken
  public Result del(@PathVariable int id) {
    if (drugstemplateService.removeById(id))
      return ResultGenerator.getSuccessResult("", "删除成功");
    return ResultGenerator.getFailResult("", "删除失败");
  }

  @DeleteMapping("/batchdel")
  @UserLoginToken
  public Result batchDel(@RequestParam String ids) {
    String[] idList = ids.split(",");
    List<Integer> list = new ArrayList<>(idList.length);
    for (String id : idList) {
      list.add(Integer.parseInt(id));
    }
    if (drugstemplateService.removeByIds(list))
      return ResultGenerator.getSuccessResult("", "删除成功");
    return ResultGenerator.getFailResult("", "删除失败");
  }
}
